package day13.homework;

import java.io.*;
import java.util.*;

public class Test{
    static Map<String,User> map = new HashMap<>();
    static Scanner sc = new Scanner(System.in);
    static {
        input();
    }
    public static void main(String[] args) {
        menu();
        System.out.println("系统结束运行");
    }
    public static void menu(){
        while (true){
            System.out.println("-----------------");
            System.out.println("1.注册用户");
            System.out.println("2.登录用户");
            System.out.println("3.退出");
            System.out.println("-----------------");
            System.out.println("请输入选择:");
            String s = sc.next();
            switch (s){
                case "1":register();break;
                case "2":login();break;
                case "3":return;
            }
        }
    }
    public static void register(){
        System.out.println("以下是随机的9个电话号码:");
        Set<String> phones = getTelephone();
        String[] telephones = phones.toArray(new String[0]);
        System.out.println("\n请选择电话号码：1-9");
        int choice = sc.nextInt();
        String telephone = telephones[choice-1];
        TalkUser talkUser = new TalkUser();
        SuperUser superUser = new SuperUser();
        InternetUser internetUser = new InternetUser();
        System.out.println("请选择你的套餐：1-3");
        talkUser.print();
        superUser.print();
        internetUser.print();
        String choice2 = sc.next();
        AbstractMessage am;
        if (choice2.equals("1")) am = new TalkUser();
        else if (choice2.equals("2")) am = new SuperUser();
        else am = new InternetUser();
        System.out.println("请输入用户名：");
        String name = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        System.out.println("请输入预存话费：");
        double money = sc.nextDouble();
        User u = new User(telephone,name,password,am,money,money,0,0,0);
        map.put(name,u);
        System.out.println("成功保存用户信息!");
        show(name);
        output();
    }
    public static void show(String name){
        for (String s:map.keySet()){
            if (s.equals(name)){
                System.out.println("卡号:"+map.get(s).getTelephoneNumber());
                System.out.println("用户名:"+s);
                System.out.println("当前余额:"+map.get(s).getBalance());
                map.get(s).getAm().print();
            }
        }
    }
    //登录
    public static void login(){

    }
    public static Set<String> getTelephone(){
        Set<String> phones =  new TreeSet<>();
        String start = "139";
        Random r = new Random();
        while (phones.size()<9){
            for (int i = 0; i < 8; i++) {
                int number = r.nextInt(10);
                start+=number;
            }
            phones.add(start);
            start = "139";
        }
        int index = 0;
        for (String s:phones){
            if (index<=2){
                System.out.print(s+" ");
                index++;
            }else {
                System.out.print("\n"+s+" ");
                index = 1;
            }
        }
        return phones;
    }
    //序列化
    public static void output(){
        try(
             FileOutputStream fos = new FileOutputStream("d://IO//a//telephoneUse.xxx");
             ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(map);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //反序列化
    public static void input(){
        try(
            FileInputStream fis = new FileInputStream("d://IO//a//telephoneUse.xxx");
            ObjectInputStream ois = new ObjectInputStream(fis)
        ) {
            map = (Map<String, User>)ois.readObject();
        }catch (FileNotFoundException e){
            System.out.println("欢迎第一次使用");
        }catch (IOException e){
            e.printStackTrace();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
